<?php

defined( 'IN_CLASS' ) or die( 'Access Error!' );

class ModelMember extends base {
	
	public function add( $aU ) { //添加一个用户
		if( count( $aU ) < 3 || !$aU['id'] || !$aU['pass'] ) {
			return 0;
		}
		
		$sql = "INSERT INTO $this->tmember SET ";
		
		foreach ( $aU as $k => $v ) {
			$sql .= "$k='$v',";
		}
		$sql = substr( $sql, 0, -1 );
		
		model::dbuser()->exe( $sql );
		return model::dbuser()->update_rows();
	}
	
	public function edit( $uid, $aU ) { //编辑个人资料
		if( count( $aU ) < 1 ) {
			return 0;
		}
		
		$sql = "UPDATE $this->tmember SET ";
		foreach ( $aU as $k => $v ) {
			$sql .= "$k='$v',";
		}
		$sql = substr( $sql, 0, -1 );
		$sql .= " WHERE id='$uid' LIMIT 1";
		
		model::dbuser()->exe( $sql );
		return model::dbuser()->update_rows();
	}
	
	/**
	 * 退出班集,与班集断开关系
	 *
	 * @param string $uid
	 * @return int
	 */
	public function out( $uid ) {
		if( !strlen( $uid ) ) {
			return 0;
		}
		
		$sql = "UPDATE $this->tmember SET power=-1 WHERE `id`='$uid' AND power<2 LIMIT 1";
		model::dbuser()->exe( $sql );
		return model::dbuser()->update_rows();
	}
	
	/**
	 * 根据用户名获取用户的详细信息
	 *
	 * @param string $uid
	 * @return array
	 */
	public function get( $uid ) {
		$sql = "SELECT * FROM $this->tmember WHERE id='$uid' LIMIT 1";
		$aU = model::dbuser()->get_one( $sql );
		( count( $aU ) > 0 ) ? '' : $aU = array();
		return $aU;
	}
	
	public function loginfresh( $id ) {
		$ip = func::getclientip();
		$sql = "UPDATE $this->tmember SET lastip='{$ip}',lastlogin=NOW(),logintimes=logintimes+1
				WHERE id='{$id}' LIMIT 1";
		model::dbuser()->exe( $sql );
	}
	
	//获取班集成员数
	public function gettotal() {
		$sql = "select count(id) as total from " . $this->tmember;
		$aStatus = model::dbuser()->get_one( $sql );
		return (int)$aStatus['total'];
	}
	
	//top10列表
	public function gettop10( $top = 10 ) {
		$sql = "select id,name,lastlogin,memberscore from $this->tmember order by memberscore desc limit {$top}";
		$aTop  = model::dbuser()->get_all( $sql );
		return $aTop;
	}
	
	//管理员列表
	public function getadminlist() {
		$sql = "select id, name from $this->tmember where power>1 order by jointime";
		$aAdmin = model::dbuser()->get_all( $sql );
		return $aAdmin;
	}
	
	//获得待批准的用户的列表
	public function getwaitlist( $limit = 3 ) {
		$sql = "SELECT id, name FROM $this->tmember WHERE power=0 ORDER BY jointime DESC LIMIT $limit ";
		$aWait = model::dbuser()->get_all( $sql );
		return $aWait;
	}
	
	//随机获取一个用户信息
	public function getrandone() {
		$sql = "select * from $this->tmember order by rand() limit 0,1";
		$aOne = model::dbuser()->get_one( $sql );
		return $aOne;
	}
	
	//获得班集全体成员
	public function getall() {
		$uid = all::$aU['id'];
		$sql = "SELECT * FROM $this->tmember WHERE id!='{$uid}' AND power>0 ORDER BY name";
		$members = model::dbuser()->get_all ( $sql );
		return $members;
	}
	
	public function getalluser() { //获得班集所有成员的信息
		$sql = "SELECT * FROM $this->tmember ORDER BY jointime ASC";
		return model::dbuser()->get_all( $sql );
	}

	public function getpart( $limit, $view = '') { //列表查看用户
		$order = ( $view != 'visitnum' ) ? 'ORDER BY id DESC' : 'ORDER BY memberscore DESC' ;
		$sql = "SELECT SQL_CALC_FOUND_ROWS * FROM $this->tmember WHERE power>0 {$order} {$limit} ";
		return model::dbuser()->get_all( $sql );
	}
	
	public function resetpwd( $uid, $opass, $pass, $repass ) {	//重设密码
		if ( empty( $opass) || empty( $pass ) || empty( $repass ) || ( strlen( $pass ) < 6 ) ) { return false ; }
		$aE = model::member()->get( $uid );
		if ( $aE['pass'] == md5( $opass ) &&  $pass == $repass ) { //旧密码正确
			$sql = "UPDATE $this->tmember SET pass=md5('{$pass}') WHERE id='{$uid}' LIMIT 1";
			model::dbuser()->exe( $sql );
			return model::dbuser()->update_rows();
		}
		
		return 0;
	}
	
	public function pass( $uid ) {
		$sql = "UPDATE $this->tmember SET power=1 WHERE id='{$uid}' AND power=0 LIMIT 1";
		model::dbuser()->exe( $sql );
		return model::dbuser()->update_rows();
	}
	
	/**
	 *  提升或者降级管理员
	 *
	 * @param string $uid 要操作的人的账号
	 * @param bool $up true为提升,false为降级
	 * @return int
	 */
	public function updatepower( $uid, $up = true ) {
		$power = $up == true ? 2 : 1;
		
		$flag = 0;
		if ( $uid ) {
			$sql = "UPDATE $this->tmember SET power='$power' WHERE id='{$uid}' AND power>0 LIMIT 1";
			model::dbuser()->exe( $sql );
			$flag = model::dbuser()->update_rows();
		}
		
		return $flag;
	}
}